package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.ExerciseProblemsDTO;
import com.fuli.app.dto.query.ExerciseProblemsQueryDTO;
import com.fuli.app.service.ExerciseProblemsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags ="习题")
@RequestMapping("/exerciseProblems")
public class ExerciseProblemsController {
    @Autowired
    private ExerciseProblemsService exerciseProblemsService;

    @ApiOperation(value = "新增习题",
            notes = "<b>必填属性：</b><br>" +
                    "1.content 内容<br/>" +
                    "2.name 习题名称<br/>" +
                    "3.curriculumId 课程id<br/>")
    @PostMapping("/add")
    public ActionResult<String> add(@ApiParam("习题DTO") @RequestBody ExerciseProblemsDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getContent()))
            errors.add(new ErrorInfo(0, "内容为空"));
        if (StringUtils.isEmpty(dto.getCurriculumId()))
            errors.add(new ErrorInfo(0, "课程id为空"));
        if (StringUtils.isEmpty(dto.getName()))
            errors.add(new ErrorInfo(0, "名称为空"));
        if (errors.size() > 0)
            return new ActionResult<>(errors);
        return exerciseProblemsService.add(dto);
    }

    @ApiOperation(value = "删除习题")
    @DeleteMapping(value = "/delete/{id}")
    public ActionResult<String> delete(@ApiParam("习题id") @PathVariable String id) {
        return exerciseProblemsService.delete(id);
    }

    @ApiOperation(value = "批量删除习题")
    @DeleteMapping(value = "/batchDelete")
    public ActionResult<List<String>> batchDelete(@ApiParam("习题id数组") @RequestParam List<String> ids) {
        if (CollectionUtils.isEmpty(ids))
            return new ActionResult<>(0, "参数不能为空");
        return exerciseProblemsService.batchDelete(ids);
    }

    @ApiOperation(
            value = "修改习题",
            notes = "<b>必填属性：</b><br>" +
                    "1.id 主键<br/>" +
                    "2.name 名称<br/>" +
                    "2.content 习题内容<br/>" +
                    "3.categoryId 课程id<br/>"
    )
    @PutMapping(value = "/update")
    public ActionResult<String> update(@ApiParam("习题DTO") @RequestBody ExerciseProblemsDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getId()))
            errors.add(new ErrorInfo(0, "id为空"));
        if (StringUtils.isEmpty(dto.getName()))
            errors.add(new ErrorInfo(0, "名称为空"));
        if (StringUtils.isEmpty(dto.getContent()))
            errors.add(new ErrorInfo(0, "内容为空"));
        if (StringUtils.isEmpty(dto.getCurriculumId()))
            errors.add(new ErrorInfo(0, "课程id为空"));
        if (!errors.isEmpty())
            return new ActionResult<>(errors);
        return exerciseProblemsService.update(dto);
    }

    @ApiOperation(value = "获取习题")
    @GetMapping(value = "/get/{id}")
    public ActionResult<ExerciseProblemsDTO> get(@ApiParam("习题id") @PathVariable String id) {
        return exerciseProblemsService.get(id);
    }
    @ApiOperation(value = "通过课程id获取习题")
    @GetMapping(value = "/getByCurriculumId/{curriculumId}")
    public ActionResult<List<ExerciseProblemsDTO>> getByCurriculumId(@ApiParam("习题id") @PathVariable String curriculumId) {
        return exerciseProblemsService.getByCurriculumId(curriculumId);
    }

    @ApiOperation("升序")
    @PutMapping("/up/{id}")
    public ActionResult<String> up(@ApiParam("id") @PathVariable String id) {
        return exerciseProblemsService.up(id);
    }

    @ApiOperation("降序")
    @PutMapping("/down/{id}")
    public ActionResult<String> down(@ApiParam("id") @PathVariable String id) {
        return exerciseProblemsService.down(id);
    }

    @ApiOperation(value = "刷新习题排序")
    @PutMapping(value = "/refresh/{curriculumId}")
    public ActionResult<String> refresh(@ApiParam("习题id") @PathVariable String curriculumId) {
        return exerciseProblemsService.refresh(curriculumId);
    }

    @ApiOperation("查询习题列表")
    @GetMapping("/pageQuery")
    public ActionResult<Pagination<ExerciseProblemsDTO>> pageQuery(@ApiParam("习题查询DTO") @ModelAttribute ExerciseProblemsQueryDTO queryDTO) {
        if (queryDTO.getPageNo() == null || queryDTO.getPageNo() <= 0)
            queryDTO.setPageNo(1);
        if (queryDTO.getPageSize() == null || queryDTO.getPageSize() <= 0)
            queryDTO.setPageSize(10);
        return exerciseProblemsService.listByQueryDTO(queryDTO);
    }
}
